using System;
using Habanero.Base;

namespace FireStarterModeller.BO
{
    /// <summary>
    /// A <see cref="IBusinessObjectRule"/> that validates that the single side of a Many-To-One <see cref="DMRelationship"/>
    /// has the Single side <see cref="DeleteParentAction"/> set to <see cref="DeleteParentAction.DoNothing"/>.
    /// </summary>
    public class DMRelationshipOneToManyAggregationCompositionMustBeOnManySide : IBusinessObjectRule
    {
        private readonly DMRelationship _dmRelationship;

        public DMRelationshipOneToManyAggregationCompositionMustBeOnManySide(DMRelationship dmRelationship)
        {
            if (dmRelationship == null) throw new ArgumentNullException("dmRelationship");
            _dmRelationship = dmRelationship;
        }

        #region Implementation of IBusinessObjectRule

        /// <summary>
        /// Indicates whether the property value is valid against the rules
        /// </summary>
        /// <returns>
        /// Returns true if valid
        /// </returns>
        public bool IsValid()
        {
            return IsOneDirectionAssociation();
        }

        private bool IsOneDirectionAssociation()
        {
            return !_dmRelationship.IsCompositionOrAggregation() || !_dmRelationship.ReverseRelationship.IsCompositionOrAggregation();
        }

        /// <summary>
        /// Returns the rule name
        /// </summary>
        public string Name
        {
            get { return "DMRelationshipOneToManyAggregationCompositionMustBeOnManySide"; }
        }

        /// <summary>
        /// Returns the error message for if the rule fails.
        /// </summary>
        public string Message
        {
            get
            {
                DMRelationship reverseRelationship = _dmRelationship.ReverseRelationship;
                return "The Relationship Type '" + _dmRelationship.RelationshipType
                       + "' on Class '" + _dmRelationship.ClassDM + "' must be on the OneToMany Side";
            }
        }

        /// <summary>
        /// The <see cref="P:Habanero.Base.IBusinessObjectRule.ErrorLevel" /> for this BusinessObjectRule e.g. Warning, Error. 
        /// </summary>
        public ErrorLevel ErrorLevel
        {
            get { return Habanero.Base.ErrorLevel.Error; }
        }

        #endregion
    }
}